﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.ViewModel;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcDataList.xaml 的交互逻辑
    /// </summary>
    public partial class UcDataList : UserControl
    {
        UcDataListVModel VModel = new UcDataListVModel();
        public UcDataList()
        {
            InitializeComponent();

            VModel.DataCountPerPage = 20;

            DateTime dt = DateTime.Now;
            DateTime jt = new DateTime(dt.Year, dt.Month, dt.Day);
            VModel.TimeGo = jt;
            VModel.TimeTo = jt.AddDays(1);

            this.DataContext = VModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            if (!WpfHelp.IsInDesignMode(this))
            {
                if (MainWindow.SelectItemNum != 12)
                    return;

                if (VModel.Nian == null)
                {
                    VModel.Nian = TestRecordDb.GetNianAll();
                }

                Updata();
            }
        }

        private void click_dc(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
            sfd.FileName = "平面度记录";
            sfd.OverwritePrompt = true;

            if (sfd.ShowDialog() == true)
            {
                string path = sfd.FileName;

                Task.Run(() =>
                {
                    try
                    {
                        VModel.Vis = Visibility.Visible;
                        Thread.Sleep(200);

                        TestRecordPageDto dto = new TestRecordPageDto();
                        //dto.PageIndex = VModel.PageIndex;
                        //dto.PageSize = VModel.DataCountPerPage;

                        dto.TimeGo = VModel.TimeGo;
                        dto.TimeTo = VModel.TimeTo;
                        dto.Project = VModel.Project;
                        dto.Work = VModel.Work;
                        dto.Ma = VModel.Ma;

                        TestRecordDb.GetPageExcel(dto, path);
                        MessageBox.Show("导出成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("导出数据失败：" + ex.Message);
                    }

                    VModel.Vis = Visibility.Collapsed;
                });
            }
        }

        private void click_cx(object sender, RoutedEventArgs e)
        {
            Updata();
        }

        private void selectChang(object sender, SelectionChangedEventArgs e)
        {

        }

        void Updata()
        {
            if (MainWindow.SelectItemNum != 12)
                return;

            Task.Run(() =>
            {
                try
                {
                    VModel.Vis = Visibility.Visible;
                    Thread.Sleep(200);

                    TestRecordPageDto dto = new TestRecordPageDto();
                    dto.PageIndex = VModel.PageIndex;
                    dto.PageSize = VModel.DataCountPerPage;

                    dto.TimeGo = VModel.TimeGo;
                    dto.TimeTo = VModel.TimeTo;
                    dto.Project = VModel.Project;
                    dto.Work = VModel.Work;
                    dto.Ma = VModel.Ma;

                    var data = TestRecordDb.GetPage(dto, out int pageCount);
                    VModel.MaxPageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(pageCount) / Convert.ToDecimal(VModel.DataCountPerPage)));
                    VModel.DataList = data;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载数据失败：" + ex.Message);
                }

                VModel.Vis = Visibility.Collapsed;
            });
        }

        private void pageChan(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            VModel.PageIndex = e.Info;
            Updata();
        }
    }
}
